package org.linlinjava.litemall.admin.web2.statistics;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.linlinjava.litemall.core.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.math.BigInteger;
import java.time.LocalDate;
import java.time.Year;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;


@Api("报表中心")
@RestController
@RequestMapping("/statistics")
public class AStatisticsController {



    @ApiOperation(value = "报表中心-每月订单量统计（已完成的订单）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "year", value = "时间，格式[yyyy]", paramType = "query")
    })
    @GetMapping("/getOrder")
    public Object getOrder(@RequestParam String year) {
        LocalDate begin = Year.parse(year).atDay(1);
        LocalDate end = begin.plusYears(1);

        Query query = em.createNativeQuery("select DATE_FORMAT(CREATED_TIME,'%Y-%m') time,count(1) from t_order where order_status in (3,4) AND :beginTime <= CREATED_TIME AND CREATED_TIME < :endTime group by time;");
        query.setParameter("beginTime", begin);
        query.setParameter("endTime", end);

        List<Object[]> resultList = query.getResultList();
        return wapper(resultList, year);
    }

    @ApiOperation(value = "报表中心-新增会员", notes = "新增会员")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "会员类型:[0-普通用户、1-个人团长、2-提货团长、3-加盟店、4-特殊加盟店、5-直营店]", paramType = "query"),
            @ApiImplicitParam(name = "year", value = "时间，格式[yyyy]", paramType = "query")
    })
    @GetMapping("/getUser")
    public Object getUser(@RequestParam(defaultValue = "0") Integer type, @RequestParam String year) {
        LocalDate begin = Year.parse(year).atDay(1);
        LocalDate end = begin.plusYears(1);

        Query query = em.createNativeQuery("select DATE_FORMAT(CREATED_TIME,'%Y-%m') time,count(1) from t_user2 where identity = :type AND :beginTime <= CREATED_TIME AND CREATED_TIME < :endTime group by time;");
        query.setParameter("type", type);
        query.setParameter("beginTime", begin);
        query.setParameter("endTime", end);

        List<Object[]> resultList = query.getResultList();
        return wapper(resultList, year);
    }

    private Object wapper(List<Object[]> resultList, String year) {
        Map<String, Integer> map = initMap(year);
        resultList.forEach(o -> {
            String key = (String) o[0];
            BigInteger v = (BigInteger) o[1];
            map.put(key, v.intValue());
        });
        return R.ok(map);
    }

    private Map<String, Integer> initMap(String year) {
        LinkedHashMap<String, Integer> map = new LinkedHashMap<>();
        map.put(year + "-01", 0);
        map.put(year + "-02", 0);
        map.put(year + "-03", 0);
        map.put(year + "-04", 0);
        map.put(year + "-05", 0);
        map.put(year + "-06", 0);
        map.put(year + "-07", 0);
        map.put(year + "-08", 0);
        map.put(year + "-09", 0);
        map.put(year + "-10", 0);
        map.put(year + "-11", 0);
        map.put(year + "-12", 0);
        return map;
    }

    @Autowired
    private EntityManager em;

}


